//
//  SFVisitorView.swift
//  SFWeiBo
/**
 let anim = CABasicAnimation(keyPath: "transform.rotation")

 anim.toValue = 2 * M_PI
 anim.repeatCount = MAXFLOAT
 anim.duration = 20

 anim.removedOnCompletion = false

 circleView.layer.addAnimation(anim, forKey: nil)*/
//  Created by 孙凡 on 16/4/8.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol SFVisitorViewDelegate:NSObjectProtocol {

    optional func visitorViewRegisterBtnDidClick(view: SFVisitorView?, button:UIButton?);
}



class SFVisitorView: UIView {

    weak var delegate: SFVisitorViewDelegate?



//    init(imageName: String, titile: String ) {
//
//        self.init()
//
//
//    }

    func setInfo(imageName: String?, message:String?){

        if imageName == nil {


        }else {

            circleView.hidden = true

            iconView.image = UIImage(named: imageName!)

            introLabel.text = message
        }

    }



    override init(frame: CGRect) {

        super.init(frame: frame)

        setupUI()
        setupAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }





    private func setupAnimation(){

        let anim = CABasicAnimation(keyPath: "transform.rotation")

        anim.toValue = 2 * M_PI

        anim.repeatCount = MAXFLOAT

        anim.duration = 20

        anim.removedOnCompletion = false


        circleView.layer.addAnimation(anim, forKey: "ratate")
    }




    private func setupUI(){

//        backgroundColor = UIColor.init(white: 237/255.0, alpha: 1)
        backgroundColor = RGB(red: 237, green: 237, blue: 237)


        //添加圆圈

        addSubview(circleView)
        addSubview(coverView)
        addSubview(iconView)
        addSubview(introLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)



        /**
         参数:
         - 1. 要约束的控件
         - 2. 约束的属性,比如中心,左边
         - 3. 相等,大于,小于 (关系)
         - 4. 相对谁来约束
         - 5. 约束的属性,比如中心,左边
         - 6. 乘以
         - 7. 偏移量
         */
        circleView.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -70))


        iconView.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -70))



        introLabel.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.size.width * 0.7))
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 44))



        registerBtn.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))


        loginBtn.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))



        coverView.snp_makeConstraints { (make) in

            make.center.equalTo(circleView)
        }
        


    }


    //懒加载控件

    //大图
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //里面的图
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    //简介
    private lazy var introLabel:UILabel = {

        let label = UILabel()

        label.text = "关注一些人回来看看这里有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)


        return label
    }()

    //注册按钮
    private lazy var registerBtn:UIButton = {

        let btn = UIButton(title: "注册", textColor: UIColor.orangeColor())

        btn.addTarget(self, action: #selector(SFVisitorView.registerDidClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()

    //登陆按钮
    private lazy var loginBtn:UIButton = {

        let btn = UIButton(title: "登陆", textColor: UIColor.darkGrayColor())

        btn.addTarget(self, action: #selector(SFVisitorView.registerDidClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)


        return btn
    }()

    //遮盖
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))



    //MARK  按钮点击事件
    @objc private func registerDidClick(button:UIButton){

        delegate?.visitorViewRegisterBtnDidClick?(self, button: button)

    }
}



